package com.system.role.entity.DTO;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import com.system.role.entity.Role;
public class RoleQueryDTO {
	private String roleName;
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public static Specification<Role> getSpecification(RoleQueryDTO roleQueryDTO){
		Specification<Role> spec = new Specification<Role>() {
			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<Predicate>();
				if(null !=roleQueryDTO &&!StringUtils.isEmpty(roleQueryDTO.getRoleName())) {
					Predicate p1 = cb.like(root.get("roleName").as(String.class), "%"+roleQueryDTO.getRoleName()+"%");
					list.add(p1);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
